package com.winchain.chainbill.trade.service.impl;

import com.shell.bee.acct.AcctBlcService;
import com.shell.bee.acct.AcctService;
import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.account.po.AcctCard;
import com.shell.bee.acct.account.util.VirtualAcctUtil;
import com.shell.bee.acct.transaction.vo.AcctInfo;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.stellar.StellarConfiguration;
import com.winchain.chainbill.ChainBillUtil;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.trade.service.BillAcctService;
import com.winchain.chainbill.trade.service.CoopBankStrategyService;
import java.math.BigDecimal;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("billAcctService")
public class BillAcctServiceImpl implements BillAcctService {
	@Autowired
	private AcctService acctService;
	@Autowired
	private CusRecService cusRecService;
	@Resource(name = "${coop.bank.strategy}")
	private CoopBankStrategyService coopBankStrategyService;
	@Autowired
	private AcctBlcService acctBlcService;

	public int add(AcctBas base) {
		return this.acctService.add(base);
	}

	public int addCard(AcctCard card) {
		return this.acctService.addCard(card);
	}

	public int update(AcctBas base) {
		return this.acctService.update(base);
	}

	public int updateCard(AcctCard card) {
		return this.acctService.updateCard(card);
	}

	public AcctBas getAcctBas(String cusId, String acctType, String acctUse) {
		if ((!StringUtil.emptyAndNull(acctUse)) && ("00".equals(acctUse))) {
			String fiNo = null;
			if ((ChainBillUtil.onlinePay()) && (VirtualAcctUtil.onVirtual())) {
				fiNo = this.coopBankStrategyService.getBankCode();
				acctType = "1";
			} else {
				acctType = "0";
			}
			AcctCard card = this.acctService.getCard(cusId, acctType, acctUse, fiNo);
			setupCardFiNm(card);
			return card;
		}
		AcctBas acctBas = this.acctService.getAcctBas(cusId, acctType, acctUse);

		return acctBas;
	}

	public AcctBas getAcctBas(String acctNo) {
		return this.acctService.getAcctBasByAcctNo(acctNo);
	}

	public AcctInfo getAcctInfo(AcctInfo acct) {
		acct.setAcctSts("N");
		return this.acctService.getAcctInfo(acct);
	}

	public List<AcctInfo> getAcctInfos(AcctInfo acct) {
		acct.setAcctSts("N");
		return this.acctService.getAcctInfos(acct);
	}

	public AcctBas getAcctBas(AcctBas bas) {
		return this.acctService.getAcctBas(bas);
	}

	public List<AcctBas> getAcctBasList(AcctBas bas) {
		return this.acctService.getAcctBasList(bas);
	}

	public AcctCard getPlatSpclAcct(String bankCode) {
		AcctCard card = new AcctCard();
		card.setCustId("platform");
		card.setFiNo(bankCode);
		card.setAcctType("1");
		card.setOaaChnCode("B");
		return this.acctService.getAcctCard(card);
	}

	public List<AcctCard> getCards(String cusId, String acctType) {
		List<AcctCard> cards = this.acctService.getCards(cusId, acctType);
		for (AcctCard card : cards) {
			setupCardFiNm(card);
		}
		return cards;
	}

	public AcctCard getAcctCard(AcctCard card) {
		return this.acctService.getAcctCard(card);
	}

	public List<AcctCard> getCards(AcctCard card) {
		return this.acctService.getCards(card);
	}

	public AcctCard getCard(String acctNo) {
		AcctCard card = this.acctService.getCard(acctNo);
		setupCardFiNm(card);
		return card;
	}

	public AcctInfo getAcctCardInfo(AcctInfo acct) {
		acct.setAcctSts("N");
		return this.acctService.getAcctCardInfo(acct);
	}

	public void openChainTransitAcct(String finId) {
		AcctBas base = getAcctBas(finId, "2", null);
		if (base == null) {
			base = new AcctBas();
			CusRec cust = (CusRec) this.cusRecService.selectByKey(finId);
			base.setAcctNo(SystemUtil.getUUID());
			base.setCustId(cust.getCusId());
			base.setCustNm(cust.getCusName());
			base.setAcctType("2");
			base.setAcctNm(cust.getCusName());

			base.setCustAcctType("C");
			base.setOaaChnCode("C");
			base.setOaaDt(DateUtil.getSystemDate());
			base.setAcctSts(String.valueOf("N"));
			base.setFiNo(null);
			base.setDataScLvl("B");

			base.setInitBkp(false);
			add(base);

			initBkp(cust, AssetCode.XQT.name(), base.getAcctNo(), BigDecimal.ZERO);
			initBkp(cust, AssetCode.XCB.name(), base.getAcctNo(),
					new BigDecimal(StellarConfiguration.getIssueAssetChain()));
		}
	}

	private void initBkp(CusRec cust, String currCode, String acctNo, BigDecimal acctAmt) {
		AcctBkp acctBkp = new AcctBkp();
		acctBkp.setAcctNo(acctNo);
		acctBkp.setPrdAccFncCls("PC");
		acctBkp.setCurrCode(currCode);
		acctBkp.setBkpPrdCode(cust.getGrpId());
		acctBkp.setBkpCttNo(cust.getFinId());
		acctBkp.setAcctAmt(acctAmt);
		this.acctBlcService.add(acctBkp);
	}

	private void setupCardFiNm(AcctCard card) {
		if (card != null) {
			StringBuilder builder = new StringBuilder();
			if (!StringUtil.emptyAndNull(card.getHqFiNm())) {
				builder.append(card.getHqFiNm());
			}
			if (!StringUtil.emptyAndNull(card.getFiNm())) {
				builder.append(card.getFiNm());
			}
			card.setFiNm(builder.toString());
		}
	}
}
